const merchantInfoDao = require('../../app/facade/merchantInfo.facade.js'),
    _ = require('lodash');

module.exports = {
    getListbystatus: getListbystatus,
    updateByStatus2: updateByStatus2,
    gets: gets,
    pass: pass,
    reject: reject,
    add: insert,
    check: check
}

function getListbystatus(req, res, next) {
    let status = req.params.status;
    if (_.isEmpty(status)) {
        res.json('status is empty');
    } else {
        merchantInfoDao.getBystatus(status, (error, result) => {
            if(error) return next(error,req,res);
            res.json(result);
        });
    }
}

function updateByStatus2(req, res, next){
    let _id = req.params.id;
        status2 = req.params.status;
    if(_.isEmpty(_id)){
        res.json('id is empty');
    } else {
        merchantInfoDao.updateStatus2(_id,status2, (error, result)=>{
            merchantInfoDao.getById(_id, (error, result)=>{
                if(error) return next(error,req,res);
                res.json(result);
            })
        });
    }
}

function gets(req, res, next){
    merchantInfoDao.gets((error, result)=>{
        if(error) return next(error,req,res);
        res.json(result);
    }); 
}

function pass(req, res, result){
    let _id = req.body.id;
    if(_.isEmpty(_id)){
        res.json('id is empty');
    } else {
        merchantInfoDao.updateStatus1(_id,'1', (error, result)=>{
             if(error) return next(error,req,res);
             res.json(result);
        });
    }
}

function reject(req, res, result){
    let _id = req.body.id,
        reason = req.body.reason;
    if(_.isEmpty(_id)){
        res.json('id is empty');
    } else {
        merchantInfoDao.updateWithReajectRea(_id,'2',reason, (error, result)=>{
             if(error) return next(error,req,res);
             res.json(result);
        });
    }
}

function insert(req, res ,result){
    let merchant  = req.body;
    if(_.isEmpty(merchant)){
        res.json('merchant data is empty');
    } else {
        merchantInfoDao.getByName(merchant.name,(error, result)=>{
            if(null!=result){
                result.registerNum = parseInt(result.registerNum)+1;
                if(merchant.icPicUrl)   result.icPicUrl = merchant.icPicUrl;
                if(merchant.portraitUrl)   result.portraitUrl = merchant.portraitUrl;
                if(merchant.icNum)   result.icNum = merchant.icNum;
                if(merchant.addr)   result.addr = merchant.addr;
                result.status1 = "0";
                result.rejectRea = "";
                result.operationTime = new Date();
                merchantInfoDao.update(result,function(){
                    if(error) return next(error,req,res);
                    res.json(result);
                })
            }else{
                merchantInfoDao.insertOne(merchant, (error, result)=>{
                    if(error) return next(error,req,res);
                    res.json(result);
                })
            }
        })
    }
}

function check(req, res ,result){
    let icnum = req.params.icnum;
    console.log("login checking : " + icnum);
    if(_.isEmpty(icnum)){
        res.json('icnum is empty');
    } else {
        merchantInfoDao.getByIcum(icnum, (error, result)=>{
            if(error) return next(error,req,res);
            let r = {
                'status':result.status1,
                'reason':result.rejectRea,
                'icnum':result.icNum
            }
            res.json(r); 
        })
    }
}
